/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Identifiedby;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Identificationelement;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Curatorialelement;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class IdentifiedbyJpaController {

    public IdentifiedbyJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Identifiedby save(Identifiedby identifiedby) {
        Identifiedby aux = findIdentifiedby(identifiedby.getIdentifiedby());
        if(aux==null){
            return create(identifiedby);
        } else
            return aux;
    }

    public Identifiedby create(Identifiedby identifiedby) {
        Identifiedby r = null;
        if (identifiedby.getIdentificationelementList() == null) {
            identifiedby.setIdentificationelementList(new ArrayList<Identificationelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Identificationelement> attachedIdentificationelementList = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListIdentificationelementToAttach : identifiedby.getIdentificationelementList()) {
                identificationelementListIdentificationelementToAttach = em.getReference(identificationelementListIdentificationelementToAttach.getClass(), identificationelementListIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementList.add(identificationelementListIdentificationelementToAttach);
            }
            identifiedby.setIdentificationelementList(attachedIdentificationelementList);
            r = em.merge(identifiedby);
            for (Identificationelement identificationelementListIdentificationelement : identifiedby.getIdentificationelementList()) {
                identificationelementListIdentificationelement.getIdentifiedbyList().add(identifiedby);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Identifiedby identifiedby) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identifiedby persistentIdentifiedby = em.find(Identifiedby.class, identifiedby.getIdidentifiedby());
            List<Identificationelement> identificationelementListOld = persistentIdentifiedby.getIdentificationelementList();
            List<Identificationelement> identificationelementListNew = identifiedby.getIdentificationelementList();
            List<Curatorialelement> curatorialelementListOld = persistentIdentifiedby.getCuratorialelementList();
            List<Curatorialelement> curatorialelementListNew = identifiedby.getCuratorialelementList();
            List<Identificationelement> attachedIdentificationelementListNew = new ArrayList<Identificationelement>();
            for (Identificationelement identificationelementListNewIdentificationelementToAttach : identificationelementListNew) {
                identificationelementListNewIdentificationelementToAttach = em.getReference(identificationelementListNewIdentificationelementToAttach.getClass(), identificationelementListNewIdentificationelementToAttach.getIdidentificationelement());
                attachedIdentificationelementListNew.add(identificationelementListNewIdentificationelementToAttach);
            }
            identificationelementListNew = attachedIdentificationelementListNew;
            identifiedby.setIdentificationelementList(identificationelementListNew);
            List<Curatorialelement> attachedCuratorialelementListNew = new ArrayList<Curatorialelement>();
            for (Curatorialelement curatorialelementListNewCuratorialelementToAttach : curatorialelementListNew) {
                curatorialelementListNewCuratorialelementToAttach = em.getReference(curatorialelementListNewCuratorialelementToAttach.getClass(), curatorialelementListNewCuratorialelementToAttach.getIdcuratorialelement());
                attachedCuratorialelementListNew.add(curatorialelementListNewCuratorialelementToAttach);
            }
            curatorialelementListNew = attachedCuratorialelementListNew;
            identifiedby.setCuratorialelementList(curatorialelementListNew);
            identifiedby = em.merge(identifiedby);
            for (Identificationelement identificationelementListOldIdentificationelement : identificationelementListOld) {
                if (!identificationelementListNew.contains(identificationelementListOldIdentificationelement)) {
                    identificationelementListOldIdentificationelement.getIdentifiedbyList().remove(identifiedby);
                    identificationelementListOldIdentificationelement = em.merge(identificationelementListOldIdentificationelement);
                }
            }
            for (Identificationelement identificationelementListNewIdentificationelement : identificationelementListNew) {
                if (!identificationelementListOld.contains(identificationelementListNewIdentificationelement)) {
                    identificationelementListNewIdentificationelement.getIdentifiedbyList().add(identifiedby);
                    identificationelementListNewIdentificationelement = em.merge(identificationelementListNewIdentificationelement);
                }
            }
            for (Curatorialelement curatorialelementListOldCuratorialelement : curatorialelementListOld) {
                if (!curatorialelementListNew.contains(curatorialelementListOldCuratorialelement)) {
                    curatorialelementListOldCuratorialelement.getIdentifiedbyList().remove(identifiedby);
                    curatorialelementListOldCuratorialelement = em.merge(curatorialelementListOldCuratorialelement);
                }
            }
            for (Curatorialelement curatorialelementListNewCuratorialelement : curatorialelementListNew) {
                if (!curatorialelementListOld.contains(curatorialelementListNewCuratorialelement)) {
                    curatorialelementListNewCuratorialelement.getIdentifiedbyList().add(identifiedby);
                    curatorialelementListNewCuratorialelement = em.merge(curatorialelementListNewCuratorialelement);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = identifiedby.getIdidentifiedby();
                if (findIdentifiedby(id) == null) {
                    throw new NonexistentEntityException("The identifiedby with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identifiedby identifiedby;
            try {
                identifiedby = em.getReference(Identifiedby.class, id);
                identifiedby.getIdidentifiedby();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The identifiedby with id " + id + " no longer exists.", enfe);
            }
            List<Identificationelement> identificationelementList = identifiedby.getIdentificationelementList();
            for (Identificationelement identificationelementListIdentificationelement : identificationelementList) {
                identificationelementListIdentificationelement.getIdentifiedbyList().remove(identifiedby);
                identificationelementListIdentificationelement = em.merge(identificationelementListIdentificationelement);
            }
            List<Curatorialelement> curatorialelementList = identifiedby.getCuratorialelementList();
            for (Curatorialelement curatorialelementListCuratorialelement : curatorialelementList) {
                curatorialelementListCuratorialelement.getIdentifiedbyList().remove(identifiedby);
                curatorialelementListCuratorialelement = em.merge(curatorialelementListCuratorialelement);
            }
            em.remove(identifiedby);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Identifiedby> findIdentifiedbyEntities() {
        return findIdentifiedbyEntities(true, -1, -1);
    }

    public List<Identifiedby> findIdentifiedbyEntities(int maxResults, int firstResult) {
        return findIdentifiedbyEntities(false, maxResults, firstResult);
    }

    private List<Identifiedby> findIdentifiedbyEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Identifiedby.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    
    public Identifiedby findIdentifiedby(String q) {
        Query query= getEntityManager().createNamedQuery("Identifiedby.findByIdentifiedby");
        query.setParameter("identifiedby",q);

        return query.getResultList().isEmpty()?null:(Identifiedby)query.getResultList().get(0);
    }

    public Identifiedby findIdentifiedby(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Identifiedby.class, id);
        } finally {
            em.close();
        }
    }

    public int getIdentifiedbyCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Identifiedby> rt = cq.from(Identifiedby.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
